package com.nl.controller.xuyi.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.util.FileUtil;
import com.nl.controller.xuyi.service.XuYiSubStationService;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequestMapping("/xuYiSubStation")
@RestController
public class XuYiSubStationController {
	@Autowired
	XuYiSubStationService xuYiSubStationService;
	
	
	
	@ApiOperation(value = "插入变电站表", notes = "插入变电站表")
	@PostMapping("subStationAdd")
	public Response subStationAdd(
			@RequestParam(value = "regionName", required = false) String regionName,
			@RequestParam(value = "lnglatList", required = false) String lnglatList,
			@RequestParam(value = "remark", required = false) String remark,
			HttpSession session, @RequestParam Map<String, Object> paramMap) {
		log.info("插入变电站表参数：{}", paramMap.toString());
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String operatorTel = user.getMsisdn();		
		String[] lngLats = lnglatList.split("\\|");
		String lng="";
		String lat="";
		String[] lnglat;
		int result = 0 ;
		int count=0;
		for(int i=0;i<lngLats.length;i++){
			lnglat=lngLats[i].split(",");
			lng = lnglat[0];
			lat = lnglat[1];
			result = xuYiSubStationService.subStationAdd("暂无关联路线",regionName, lng,lat, remark, operatorTel);
			if(result>0){
				count++;
			}
		}

		log.debug("插入变电站表返回参数：{}", result);
		if (count > 0) {
			return ResponseUtil.sucess("点位添加成功，该点位包含"+count+"个经纬度");
		} else {
			log.debug("插入变电站表返回参数：{}", result);
			return ResponseUtil.error(result, "插入变电站表失败，请重试");
		}
	}
	
	@ApiOperation(value = "根据点位名称查询点位列表", notes = "根据点位名称查询点位列表")
	@PostMapping("findlatlngListByRegionName")
	public Response findlatlngListByRegionName(
			@RequestParam(value = "regionName", required = false) String regionName,
			HttpSession session, @RequestParam Map<String, Object> paramMap) {
		log.info("根据点位名称查询点位列表参数：{}", paramMap.toString());
			String latlngList = xuYiSubStationService.findlatlngListByRegionName(regionName);

		log.debug("根据点位名称查询点位列表：{}", latlngList);
		return ResponseUtil.sucess(latlngList);
	}
	
	
	@ApiOperation(value = "路线关联点位插入变电站表", notes = "路线关联点位插入变电站表")
	@PostMapping("regionRelateAdd")
	public Response regionRelateAdd(
			@RequestParam(value = "pathName", required = false) String pathName	,
			@RequestParam(value = "regionTips", required = false) String regionList,
			HttpSession session, @RequestParam Map<String, Object> paramMap) {
		log.info("路线关联点位插入变电站表：{}", paramMap.toString());
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String operatorTel = user.getMsisdn();		

		int result = 0;
		
		result = xuYiSubStationService.regionRelateAdd(pathName,regionList,operatorTel);

		log.debug("插入变电站表返回参数：{}", result);
		if (result > 0) {
			return ResponseUtil.sucess("点位关联成功，该路线下关联"+result+"个点位");
		} else {
			log.debug("路线关联点位插入变电站表返回参数：{}", result);
			return ResponseUtil.error(result, "路线关联点位插入变电站表，请重试");
		}
	}
	
	
	@ApiOperation(value = "修改变电站表", notes = "修改变电站表")
	@PostMapping("subStationModify")
	public Response subStationModify(
			@RequestParam(value = "regionName", required = false) String regionName,
			@RequestParam(value = "remark", required = false) String remark,
			@RequestParam(value = "subSeq", required = false) String subSeq,
			HttpSession session, @RequestParam Map<String, Object> paramMap) {
		log.info("插入变电站表参数：{}", paramMap.toString());
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String operatorTel = user.getMsisdn();
		int result = 0 ;
		result = xuYiSubStationService.subStationModify(regionName, remark, subSeq,operatorTel);
		
		log.debug("插入变电站表返回参数：{}", result);
		if (result == 1) {
			return ResponseUtil.sucess("添加成功");
		} else {
			log.debug("插入变电站表返回参数：{}", result);
			return ResponseUtil.error(result, "插入变电站表失败，请重试");
		}
	}
	
	@ApiOperation(value = "修改项目状态", notes = "修改项目状态")
	@PostMapping("deleteSubStation")
	private Response deleteSubStation(
			@RequestParam(value = "pathName") String pathName,
			@RequestParam(value = "regionName") String regionName,
			@RequestParam Map<String, Object> paramMap) {
		int result = xuYiSubStationService.deleteSubStation(pathName,regionName);
		if(result>0){
			log.info("删除变电站点位信息:{}" ,paramMap.toString());
			log.info("删除变电站点位信息：",result);
			return ResponseUtil.sucess("success");
		} else {
			log.error("删除变电站点位信息：{}", result);
			return ResponseUtil.error(-1, "删除变电站点位信息失败，请重试");
		}

	}

	@PostMapping("/importSubStation")
    public Response importSubStation(
            
            HttpServletRequest request,
            HttpSession session,
            @RequestParam("file") MultipartFile file
    ) {
            	if (file!=null) {
                	//导入基站信息
                	Map<String, Object> map = new HashMap<>();
                    SmsUsers user = (SmsUsers) request.getSession().getAttribute("user");
                    String now = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
                    String fileName = user.getUserId() + now+"subStation.csv";
                    String operatorTel = user.getMsisdn();
                    String path = GlobalConst.FILE_SUB_STATION_PATH;
                    try {
                        FileUtil.fileUpload(file,request,path,fileName); //文件上传
                        log.info("文件上传成功");
                        log.info("文件名"+fileName);
                    }catch (Exception e){
                        e.printStackTrace();
                        log.error("文件上传异常");
                        return ResponseUtil.error(1, "文件上传异常");
                    }
                        try {
                            int result= xuYiSubStationService.saveSubFileData(path+"/"+fileName,operatorTel); //文件解析
                            return ResponseUtil.sucess(result);
                        } catch (Exception e) {
                            e.printStackTrace();
                            map.put("code", 1);
                            log.error("数据异常请检查");
                            return ResponseUtil.error(1, "数据异常请检查");
                        }         
                } else {
                    return ResponseUtil.error(-1, "添加失败请重试");
                }
    }
	
	@RequestMapping(value = "/modeldownload2", method = RequestMethod.GET)
    public void download(HttpServletResponse res) throws Exception {
        String fileName = "SubStation.csv";
        FileUtil.download(fileName,res);
    }

    @RequestMapping(value = "/apidownload2", method = RequestMethod.GET)
    public void download1(HttpServletResponse res) throws Exception {
        String fileName = "SubStationImportApi.docx";
        FileUtil.download(fileName,res);
    }
	
}
